import axios from 'axios'
import Cache from './cache'
const myaxios = axios.create({
    // ...
})
myaxios.interceptors.request.use(config => {
    config.baseURL = process.env.baseUrl
    if(process.browser){
        const Authorization = Cache.get('Authorization')
        if(Authorization){
            config.headers['Authorization'] = Authorization
        }
    }
    return config
})
myaxios.interceptors.response.use(response => {

    return response.data
}, err => {
    
    if(err['response']&&err['response']['data']&&err['response']['data']['msg']){
        return {
            code: err['response']['data']['code'],
            msg: err['response']['data']['msg']
        }
    }else{
        
        if(err['response']['status']==404){
            return {
                code: 404,
                msg: '404 该地址无效！！！'
            }
        }else if(err['response']['status']==401){
            return {
                code: 401,
                msg: '401 没有权限！！！'
            }
        } else{
            console.error(err)
            return {
                code: 500,
                msg: '出现了点小问题，请稍后再试！！！'
            }
        }
    }
})
export default myaxios